package com.bydq.patrolsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bydq.patrolsystem.entity.*;
import com.bydq.patrolsystem.service.*;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/solver")
public class SolvetaskController {

    @Autowired
    private PsSolvetaskMainService psSolvetaskMainService;

    @Autowired
    private PsSystemconfigService psSystemconfigService;

    @Autowired
    private PsUserService psUserService;

    @Autowired
    private PsInspectiontaskDetailService psInspectiontaskDetailService;



    /**
     * 页面跳转至消缺任务指定与分配页面solveTask.xml
     * @return
     */
    @RequestMapping("/solveTask")
    public String solveTask(Model m,HttpSession session){
        m.addAttribute("user",session.getAttribute("user"));
        return "solveTask";
    }

    /**
     * 消缺任务列表页
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/solveList")
    public Map<String,Object> poleList(String solveTaskCode,String solveTaskName, Long taskStatus,
                                       String issuedByName, String startDate,String endDate,
                                       Integer page, Integer limit) {
        System.out.println("solveTaskCode:"+solveTaskCode);
//        System.out.println("lineCode:"+lineCode);
//        System.out.println("poleCode:"+poleCode);
//        System.out.println("discovererName:"+discovererName);
        IPage<PsSolvetaskMain> pages = psSolvetaskMainService.selectSolvetaskListByPaging(solveTaskCode,solveTaskName, taskStatus,
                issuedByName, startDate,endDate,
                page, limit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", pages.getTotal());
        map.put("data", pages.getRecords());
//        System.out.println(map.toString());
        return map;
    }

    /**
     * 查询任务状态下拉列表集合
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/taskStatusList")
    public List<PsSystemconfig> taskStatusList(){
        String configCode = "TASK_STATUS";
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
//        System.out.println(psSystemconfigService.list(qw));
        return psSystemconfigService.list(qw);
    }

    /**
     * 工作单据类型下拉列表集合
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/workDocType")
    public List<PsSystemconfig> workDocType(){
        String configCode = "DOC_TYPE";
        QueryWrapper<PsSystemconfig> qw=new QueryWrapper<>();
        qw.eq("configCode",configCode);
//        System.out.println(psSystemconfigService.list(qw));
        return psSystemconfigService.list(qw);
    }


    /**
     * 工作单据类型下拉列表集合
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/taskManagerCode")
    public List<PsUser> taskManagerCode(){
        QueryWrapper<PsUser> qw=new QueryWrapper<>();
        qw.eq("roleId",2);
//        System.out.println(psSystemconfigService.list(qw));
        return psUserService.list(qw);
    }


    /**
     * 消缺员弹窗
     * @return
     */
    @RequestMapping("/addSlover")
    public String addSlover( ){
        return "addSlover";
    }


    /**
     * 添加或修改消缺任务保存至数据库
     * @param
     * @return
     */
    @RequestMapping("/solveTaskSave")
    @ResponseBody
    public boolean solveTaskSave(PsSolvetaskMain psSolvetaskMain, HttpSession session){
        System.out.println("getSolveTaskCode:"+psSolvetaskMain.getSolveTaskCode());
        System.out.println("getSolveTaskName:"+psSolvetaskMain.getSolveTaskName());
        boolean flag = false;
        if(psSolvetaskMain.getId()!=null){
            System.out.println("修改");
            flag = psSolvetaskMainService.updateById(psSolvetaskMain);
        }else{
          System.out.println("添加");
          long taskStatus = 1;
            psSolvetaskMain.setTaskStatusName("待分配");
            psSolvetaskMain.setTaskStatus(taskStatus);
            flag = psSolvetaskMainService.save(psSolvetaskMain);
        }
        return flag;
    }


    /**
     * 缺陷信息列表弹窗
     * @return
     */
    @RequestMapping("/bugInfoAdd")
    public String bugInfoAdd( ){
        return "bugInfoList";
    }


    /**
     * 查询缺陷信息列表 0
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/bugInfoList")
    public Map<String,Object> bugInfoList(Integer page,Integer limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 3 : limit;
        IPage<PsInspectiontaskDetail> pages = new Page<>(page,limit);
        QueryWrapper<PsInspectiontaskDetail> qw=new QueryWrapper<>();
        qw.eq("isBug",1).eq("isSolve",0).eq("isBugLevel",1)
            .eq("taskStatus",4).eq("isBugAllocate",0);
        psInspectiontaskDetailService.page(pages,qw);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pages.getTotal());
        map.put("data", pages.getRecords());
        // System.out.println(map.toString());
        return map;
    }

    /**
     * 查询缺陷信息列表 1
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/bugInfoListStatus")
    public Map<String,Object> bugInfoListStatus(Integer page,Integer limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 3 : limit;
        IPage<PsInspectiontaskDetail> pages = new Page<>(page,limit);
        QueryWrapper<PsInspectiontaskDetail> qw=new QueryWrapper<>();
        qw.eq("isBug",1).eq("isSolve",0).eq("isBugLevel",1)
            .eq("taskStatus",4).eq("isBugAllocate",1);
        psInspectiontaskDetailService.page(pages,qw);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",pages.getTotal());
        map.put("data", pages.getRecords());
        // System.out.println(map.toString());
        return map;
    }

  /**
   * 修改是否分配任务
   * @param
   * @return
   */
  @RequestMapping("/isBugAllocate")
  @ResponseBody
  public boolean isBugAllocate(@RequestBody List<PsInspectiontaskDetail> list, HttpSession session){
    System.out.println("list:"+list.size());
    boolean flag = false;
    for(PsInspectiontaskDetail psInspectiontaskDetail:list){
      System.out.println("修改");
      psInspectiontaskDetail.setIsBugAllocate(1);
      flag = psInspectiontaskDetailService.updateById(psInspectiontaskDetail);
    }
    return flag;
  }

    /**
     * 消缺员列表
     * @param
     * @return
     */
    @RequestMapping("/solver_list")
    @ResponseBody
    public List<PsUser> solver_list() throws JSONException {
        QueryWrapper<PsUser> qw=new QueryWrapper<>();
        qw.eq("roleId",4);
        List<PsUser> solverList = psUserService.list(qw);
        return solverList;
    }


}
